package com.demo.work.util;

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 19042980
 */
@Data
@Accessors(chain = true)
@Slf4j
public class XSSFWorkbookWrapper {

    private XSSFWorkbook workbook;

    private XSSFSheet sheet;

    private XSSFCellStyle style;

    public XSSFWorkbookWrapper(File file) {

        if (file == null){
            throw new RuntimeException("文件不能为空");
        }

        if (!file.exists()){
            throw new RuntimeException("文件未找到");
        }

        try {
            this.workbook = new XSSFWorkbook(new FileInputStream(file));
        } catch (IOException e) {
            throw new RuntimeException("打开文件异常");
        }
    }

    public XSSFWorkbookWrapper() {
        this.workbook = new XSSFWorkbook();
    }

    public XSSFWorkbookWrapper useStyle(XSSFCellStyle style){
        this.style = style;
        return this;
    }

    public XSSFWorkbookWrapper clearStyle(XSSFCellStyle style){
        this.style = null;
        return this;
    }

    public XSSFWorkbookWrapper useSheet(String sheetName){
        XSSFSheet sheet = this.workbook.getSheet(sheetName);
        if (sheet == null){
            this.sheet = this.workbook.createSheet(sheetName);
        }else {
            this.sheet = sheet;
        }
        return this;
    }

    public XSSFWorkbookWrapper setValue(String columnIndex, int rowIndex, Object value){
        if (this.sheet == null){
            throw new RuntimeException("haven't use sheet yet");
        }

        if (value == null){
            return this;
        }

        XSSFRow row = this.sheet.getRow(rowIndex);
        if (row == null){
            row = this.sheet.createRow(rowIndex);
        }
        XSSFCell cell = row.getCell(ExcelUtil.getIndex(columnIndex));
        if (cell == null){
            cell = row.createCell(ExcelUtil.getIndex(columnIndex));
        }
        if (value.getClass().equals(String.class)){
            cell.setCellValue((String)value);
        }else if (value.getClass().equals(Integer.class)){
            cell.setCellValue((Integer)value);
        }else if (value.getClass().equals(Double.class)){
            cell.setCellValue((Double)value);
        }else if (value.getClass().equals(Boolean.class)){
            cell.setCellValue((Boolean)value);
        }else if (value.getClass().equals(Date.class)){
            cell.setCellValue((Date)value);
        }else if (value.getClass().equals(LocalDate.class)){
            cell.setCellValue((LocalDate)value);
        }else if (value.getClass().equals(LocalDateTime.class)){
            cell.setCellValue((LocalDateTime)value);
        }else if (value.getClass().equals(Calendar.class)){
            cell.setCellValue((Calendar)value);
        }else if (value.getClass().equals(RichTextString.class)){
            cell.setCellValue((RichTextString)value);
        }else{
            throw new RuntimeException("not accept value type [" + value.getClass().getName() + "]");
        }
        if (this.style != null){
            cell.setCellStyle(style);
        }

        return this;
    }

    public void save(File file){
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    private boolean isEmpty(String str){
        return str == null || str.isEmpty();
    }

}
